import scoreApi from 'api/score'

let state = {
  classess: {
    list: [],
    params: '',
    loading: true
  },
  students: {
    list: [],
    params: '',
    loading: true
  },
  stuDetail: {},
  itemScoreRuleInfo: {},
  scoreStandard: {}
}

let actions = {
  classess ({commit, state}, params) {
    state.classess.loading = true
    scoreApi.getTestClassess(params).then(({data = {}}) => {
      commit('CLASSESS', {data, params})
    })
  },
  students ({commit, state}, params) {
    state.students.loading = true
    return scoreApi.getTestStudents(params).then(({data = {}}) => {
      commit('STUDENTS', {data, params})
    })
  },
  stuDetail ({commit, state}, params) {
    let stuDetail = scoreApi.getStudentsDetail(params)
    stuDetail.then(({data = {}}) => {
      commit('STU_DETTAIL', data)
    })
    return stuDetail
  },
  itemScoreRuleInfo ({commit, state}, params) {
    let promise = scoreApi.getItemScoreRuleInfo(params)
    promise.then(({data = {}}) => {
      commit('ITEM_SCORE_RULE_INFO', data)
    })
    return promise
  },
  scoreStandard ({commit, state}, params) {
    let promise = scoreApi.getScoreStandard(params)
    promise.then(({data = {}}) => {
      commit('SCORE_SDANDART', data)
    })
    return promise
  },
  saveTestScore ({commit, state}, data, options) {
    let stuDetail = state.stuDetail
    let itemInfo = state.itemScoreRuleInfo
    let scoureDetaiList = state.itemScoreRuleInfo.scoureDetaiList
    let subData = Object.assign({}, {
      extraTestScores: [], // 只有id 需要遍历拿取其他信息
      studentId: stuDetail.id,
      testLevelId: stuDetail.testAgeId, // 年龄组id
      testDate: '',
      testScoreDesc: itemInfo.unitName, // 单位
      testScoreType: itemInfo.testScoreType // 类型 主观 非主观
    }, data)
    // 额外加分项数据处理
    if (subData.extraTestScores.length) {
      let extraTestScores = []
      scoureDetaiList.forEach(k => {
        if (subData.extraTestScores.indexOf(k.id) !== -1) {
          extraTestScores.push({
            testMethodId: k.id - 0,
            testScore: k.score - 0, // 转为数值型
            testScoreDesc: k.unit
          })
        }
      })
      subData.extraTestScores = extraTestScores
    }
    let promise = scoreApi.saveTestScore(JSON.stringify(subData), options)
    return promise
  }
}

let mutations = {
  EMPTY (state, {key, value}) {
    // 默认列表初始化
    state[key] = value || {
      list: [],
      params: '',
      loading: true
    }
  },
  CLASSESS (state, {data, params}) {
    data.list = params.page === 1 ? data.list : state.classess.list.concat(data.list)
    data.loading = !data.hasNextPage // 如果还有下一页 则可继续滚动
    data.params = params
    state.classess = data
  },
  STUDENTS (state, {data, params}) {
    data.list = params.page === 1 ? data.list : state.students.list.concat(data.list)
    data.loading = !data.hasNextPage // 如果还有下一页 则可继续滚动
    data.params = params
    state.students = data
  },
  STU_DETTAIL (state, data) {
    state.stuDetail = data
  },
  ITEM_SCORE_RULE_INFO (state, data) {
    data.scoureDetaiList = data.scoureDetaiList ? data.scoureDetaiList.map(k => {
      k.id = k.id + ''
      return k
    }) : []
    state.itemScoreRuleInfo = data
  },
  SCORE_SDANDART (state, data) {
    state.scoreStandard = data
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
